package lib.quasar.widget.text;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 左右有两个图标
 * created by kalu on 2018/3/26 13:46
 */
public class ImageTextView extends AppCompatTextView {

    // 图标绘制区域
    private final Rect mRectLeft = new Rect();
    private final Rect mRectRight = new Rect();
    private final Paint mPaint = new Paint();
    // 图标
    private Bitmap mBitmapLeft;
    private Bitmap mBitmapRight;
    private float mBitmapLeftWidth;
    private float mBitmapLeftHeight;
    private float mBitmapRightWidth;
    private float mBitmapRightHeight;
    private float mBitmapLeftPaddingLeft;
    private float mBitmapRightPaddingRight;
    private boolean localtionTop = false;
    private boolean localtionBottom = false;
    private boolean localtionLeft = false;
    private boolean localtionRight = false;
    private boolean paintBold = false;

    private float paddingTop = 0;
    private float paddingBottom = 0;
    private float paddingLeft = 0;
    private float paddingRight = 0;

    private float lineHeight = 5;
    private int lineColor = Color.BLACK;

    private int bgColor = Color.WHITE;
    private int bgColorPressed = Color.WHITE;

    /*******************************************************************************/

    public ImageTextView(Context context) {
        this(context, null, 0);
        // Log.e("uuuuu", "ImageTextView 111");
    }

    public ImageTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        //  Log.e("uuuuu", "ImageTextView 222");
    }

    public ImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //   Log.e("uuuuu", "ImageTextView 333");

        TypedArray array = null;
        try {

            Context applicationContext = context.getApplicationContext();
            array = applicationContext.obtainStyledAttributes(attrs, R.styleable.ImageTextView);

            BitmapDrawable leftDrawable = (BitmapDrawable) array.getDrawable(R.styleable.ImageTextView_itv_image_left);
            // Log.e("uuuuu", "leftDrawable 111");
            if (null != leftDrawable) {
                //   Log.e("uuuuu", "leftDrawable 222");
                mBitmapLeft = leftDrawable.getBitmap();
            }

            BitmapDrawable rightDrawable = (BitmapDrawable) array.getDrawable(R.styleable.ImageTextView_itv_image_right);
            // Log.e("uuuuu", "rightDrawable 111");
            if (null != rightDrawable) {
                //  Log.e("uuuuu", "rightDrawable 222");
                mBitmapRight = rightDrawable.getBitmap();
            }

            mBitmapLeftPaddingLeft = array.getDimension(R.styleable.ImageTextView_itv_image_left_padding_left, 0f);
            mBitmapRightPaddingRight = array.getDimension(R.styleable.ImageTextView_itv_image_right_padding_right, 0f);
            mBitmapLeftWidth = array.getDimension(R.styleable.ImageTextView_itv_image_left_width, 0f);
            mBitmapLeftHeight = array.getDimension(R.styleable.ImageTextView_itv_image_left_height, 0f);
            mBitmapRightWidth = array.getDimension(R.styleable.ImageTextView_itv_image_right_width, 0f);
            mBitmapRightHeight = array.getDimension(R.styleable.ImageTextView_itv_image_right_height, 0f);

            localtionTop = array.getBoolean(R.styleable.ImageTextView_itv_location_top, false);

            localtionTop = array.getBoolean(R.styleable.ImageTextView_itv_location_top, false);
            localtionBottom = array.getBoolean(R.styleable.ImageTextView_itv_location_bottom, false);
            localtionLeft = array.getBoolean(R.styleable.ImageTextView_itv_location_left, false);
            localtionRight = array.getBoolean(R.styleable.ImageTextView_itv_location_right, false);
            paintBold = array.getBoolean(R.styleable.ImageTextView_itv_paint_bold, false);

            paddingTop = array.getDimension(R.styleable.ImageTextView_itv_padding_top, 0);
            paddingBottom = array.getDimension(R.styleable.ImageTextView_itv_padding_bottom, 0);
            paddingLeft = array.getDimension(R.styleable.ImageTextView_itv_padding_left, 0);
            paddingRight = array.getDimension(R.styleable.ImageTextView_itv_padding_right, 0);

            lineHeight = array.getDimension(R.styleable.ImageTextView_itv_line_height, lineHeight);
            lineColor = array.getColor(R.styleable.ImageTextView_itv_line_color, lineColor);

            bgColor = array.getColor(R.styleable.ImageTextView_itv_bg_color, bgColor);
            bgColorPressed = array.getColor(R.styleable.ImageTextView_itv_bg_color_pressed, bgColorPressed);

        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    /*******************************************************************************/


    @Override
    protected void onDraw(Canvas canvas) {

        final int[] ints1 = {android.R.attr.state_pressed};
        final int[] ints2 = {android.R.attr.state_enabled};
        final int[][] ints3 = new int[][]{ints1, ints2};
        final StateListDrawable drawable = new StateListDrawable();
        final ColorDrawable colorDrawable1 = new ColorDrawable();
        final ColorDrawable colorDrawable2 = new ColorDrawable();
        colorDrawable1.setColor(bgColorPressed);
        colorDrawable2.setColor(bgColor);
        drawable.addState(ints3[0], colorDrawable1);
        drawable.addState(ints3[1], colorDrawable2);
        setBackgroundDrawable(drawable);

        if (paintBold) {
            getPaint().setFakeBoldText(true);
        }

        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setFakeBoldText(true);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(lineColor);

        final int height = getHeight();
        final int height2 = getHeight() - getPaddingBottom();
        final int width = getWidth();
        //    Log.e("uuuuu", "画图标onDraw");

        // 画图标
        if (null != mBitmapLeft) {

            mRectLeft.left = (int) mBitmapLeftPaddingLeft;
            mRectLeft.right = (int) (mRectLeft.left + mBitmapLeftWidth);
            mRectLeft.top = (int) ((height2 - mBitmapLeftHeight) / 2);
            mRectLeft.bottom = (int) (mBitmapLeftHeight + mRectLeft.top);
            canvas.drawBitmap(mBitmapLeft, null, mRectLeft, mPaint);
            //  Log.e("uuuuu", "画图标11");
        }
        if (null != mBitmapRight) {

            mRectRight.right = (int) (width - mBitmapRightPaddingRight);
            mRectRight.left = (int) (mRectRight.right - mBitmapRightWidth);
            mRectRight.top = (int) ((height2 - mBitmapRightHeight) / 2);
            mRectRight.bottom = (int) (mBitmapRightHeight + mRectRight.top);
            canvas.drawBitmap(mBitmapRight, null, mRectRight, mPaint);
            //  Log.e("uuuuu", "画图标22");
        }

        if (localtionTop) {
            canvas.drawRect(paddingLeft, 0, width - paddingRight, lineHeight, mPaint);
        }

        if (localtionBottom) {
            canvas.drawRect(paddingLeft, height - lineHeight, width - paddingRight, height, mPaint);
        }

        if (localtionLeft) {
            canvas.drawRect(paddingLeft, paddingTop, lineHeight, height - paddingBottom, mPaint);
        }

        if (localtionRight) {
            canvas.drawRect(width - paddingRight - lineHeight, paddingTop, width - paddingRight, height - paddingBottom, mPaint);
        }
    }
}
